home *** CD-ROM | disk | FTP | other *** search
- unit StrStuff;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls;
-
- type
- TForm1 = class(TForm)
- Edit1: TEdit;
- Label1: TLabel;
- Button1: TButton;
- Button2: TButton;
- Label2: TLabel;
- Label3: TLabel;
- Label4: TLabel;
- Button3: TButton;
- procedure Button1Click(Sender: TObject);
- procedure Button2Click(Sender: TObject);
- procedure Button3Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- TestStr : string;
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
-
- procedure AAGetStringData(const aSt : string;
- var aLen : integer;
- var aRefCount : integer;
- var aAllocSize: integer);
- type
- PLongint = ^longint;
- var
- StringPtr : PLongint;
- begin
- if (aSt = '') then begin
- aLen := 0;
- aRefCount := -1;
- aAllocSize := -1;
- end
- else begin
- StringPtr := pointer(aSt);
- dec(PChar(StringPtr), sizeof(longint));
- aLen := StringPtr^;
- dec(PChar(StringPtr), sizeof(longint));
- aRefCount := StringPtr^;
- if (aRefCount = -1) then
- aAllocSize := -1
- else begin
- dec(PChar(StringPtr), sizeof(longint));
- aAllocSize := StringPtr^ and $7FFFFFFC;
- end;
- end;
- end;
-
- procedure AAIncStringRefCount(var aSt : string);
- type
- PLongint = ^longint;
- var
- StringPtr : PLongint;
- begin
- if (aSt <> '') then begin
- StringPtr := pointer(aSt);
- dec(PChar(StringPtr), 2 * sizeof(longint));
- if (StringPtr^ <> -1) then
- inc(StringPtr^);
- end;
- end;
-
- procedure AADecStringRefCount(var aSt : string);
- type
- PLongint = ^longint;
- var
- StringPtr : PLongint;
- begin
- if (aSt <> '') then begin
- StringPtr := pointer(aSt);
- dec(PChar(StringPtr), 2 * sizeof(longint));
- if (StringPtr^ = 1) then
- aSt := ''
- else if (StringPtr^ > 1) then
- dec(StringPtr^);
- end;
- end;
-
- procedure TForm1.Button1Click(Sender: TObject);
- var
- Len, RefCount, AllocSize : longint;
- begin
- TestStr := Edit1.Text;
- Edit1.Text := '';
- AAGetStringData(TestStr, Len, RefCount, AllocSize);
- Label2.Caption := IntToStr(Len);
- Label3.Caption := IntToStr(RefCount);
- Label4.Caption := IntToStr(AllocSize);
- end;
-
- procedure TForm1.Button2Click(Sender: TObject);
- var
- Len, RefCount, AllocSize : longint;
- begin
- AAIncStringRefCount(TestStr);
- AAGetStringData(TestStr, Len, RefCount, AllocSize);
- Label2.Caption := IntToStr(Len);
- Label3.Caption := IntToStr(RefCount);
- Label4.Caption := IntToStr(AllocSize);
- end;
-
- procedure TForm1.Button3Click(Sender: TObject);
- var
- Len, RefCount, AllocSize : longint;
- begin
- AADecStringRefCount(TestStr);
- AAGetStringData(TestStr, Len, RefCount, AllocSize);
- Label2.Caption := IntToStr(Len);
- Label3.Caption := IntToStr(RefCount);
- Label4.Caption := IntToStr(AllocSize);
- end;
-
- end.
-